import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'pages/chapter10/combination_widget_route.dart';
import 'pages/chapter10/custom_paint_route.dart';
import 'pages/chapter10/gradient_circular_progress_route.dart';
import 'pages/chapter10/turn_box_route.dart';
import 'pages/chapter11/dio_test_route.dart';
import 'pages/chapter11/file_operation_route.dart';
import 'pages/chapter11/http_test_route.dart';
import 'pages/chapter11/json_test_route.dart';
import 'pages/chapter11/socket_test_route.dart';
import 'pages/chapter11/web_socket_test_route.dart';
import 'pages/chapter12/chapter12_page.dart';
import 'pages/chapter13/chapter13_page.dart';
import 'pages/chapter14/chapter14_page.dart';
import 'pages/chapter15/chapter15_page.dart';
import 'pages/chapter8/event_bus_route.dart';
import 'pages/chapter8/gesture_recognizer_route.dart';
import 'package:logger/logger.dart';
import 'pages/chapter1/chapter1_page.dart';
import 'pages/chapter2/assets_route.dart';
import 'pages/chapter2/chapter2_page.dart';
import 'pages/chapter2/counter_widget.dart';
import 'pages/chapter2/cupertino_test_route.dart';
import 'pages/chapter2/debugger_route.dart';
import 'pages/chapter2/get_state_object_route.dart';
import 'pages/chapter2/random_words_route.dart';
import 'pages/chapter2/tap_box_page.dart';
import 'pages/chapter2/tip_route.dart';
import 'pages/chapter3/button_route.dart';
import 'pages/chapter3/chapter3_page.dart';
import 'pages/chapter3/form_test_route.dart';
import 'pages/chapter3/image_icon_route.dart';
import 'pages/chapter3/progress_animation_route.dart';
import 'pages/chapter3/progress_indicator_route.dart';
import 'pages/chapter3/switch_and_checkbox_route.dart';
import 'pages/chapter3/text_field_route.dart';
import 'pages/chapter3/text_style_route.dart';
import 'pages/chapter4/align_route.dart';
import 'pages/chapter4/chapter4_page.dart';
import 'pages/chapter4/constraints_route.dart';
import 'pages/chapter4/flex_route.dart';
import 'pages/chapter4/layout_route.dart';
import 'pages/chapter4/row_and_column_route.dart';
import 'pages/chapter4/stack_and_positioned_route.dart';
import 'pages/chapter4/wrap_route.dart';
import 'pages/chapter5/chapter5_page.dart';
import 'pages/chapter5/clip_route.dart';
import 'pages/chapter5/container_route.dart';
import 'pages/chapter5/decorated_box_route.dart';
import 'pages/chapter5/fitted_box_route.dart';
import 'pages/chapter5/padding_route.dart';
import 'pages/chapter5/scaffold_route.dart';
import 'pages/chapter5/transform_route.dart';
import 'pages/chapter6/animated_list_route.dart';
import 'pages/chapter6/chapter6_page.dart';
import 'pages/chapter6/custom_scroll_view_test_route.dart';
import 'pages/chapter6/grid_view_test_route.dart';
import 'pages/chapter6/keep_alive_test_route.dart';
import 'pages/chapter6/list_view_route.dart';
import 'pages/chapter6/nested_scroll_view_route.dart';
import 'pages/chapter6/page_view_test_route.dart';
import 'pages/chapter6/scroll_controller_test_route.dart';
import 'pages/chapter6/scroll_notification_test_route.dart';
import 'pages/chapter6/single_child_scroll_view_route.dart';
import 'pages/chapter6/tab_bar_view_test_route.dart';
import 'pages/chapter7/alert_dialog_route.dart';
import 'pages/chapter7/chapter7_page.dart';
import 'pages/chapter7/future_builder_route.dart';
import 'pages/chapter7/inherited_widget_route.dart';
import 'pages/chapter7/value_listenable_route.dart';
import 'pages/chapter7/willpop_scope_test_route.dart';
import 'pages/chapter8/chapter8_page.dart';
import 'pages/chapter8/notification_route.dart';
import 'pages/chapter8/pointer_move_indicator_route.dart';
import 'pages/chapter9/animated_decorated_box_route.dart';
import 'pages/chapter9/animated_switcher_counter_route.dart';
import 'pages/chapter9/animated_widgets_route.dart';
import 'pages/chapter9/chapter9_page.dart';
import 'pages/chapter10/chapter10_page.dart';
import 'pages/chapter11/chapter11_page.dart';
import 'pages/chapter9/hero_animation_route.dart';
import 'pages/chapter9/scale_animation_route.dart';
import 'pages/chapter9/stagger_animation_route.dart';
import 'widgets/title_button.dart';

void main() {
  runApp(const MyApp());
}

var title = 'Flutter Study';

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: title,
      theme: ThemeData(brightness: Brightness.light, fontFamily: 'Roboto'),
      builder: BotToastInit(),
      debugShowCheckedModeBanner: false,
      navigatorObservers: [BotToastNavigatorObserver()],
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('zh', 'CN'),
        Locale('en', 'US'),
      ],
      //注册路由表
      routes: {
        "/": (context) => IndexPage(title: title),
        "/chapter1": (context) => const Chapter1Page(),
        "/chapter2": (context) => Chapter2Page(),
        "/chapter2/counter": (context) => const CounterWidget(),
        "/chapter2/cupertino": (context) => const CupertinoTestRoute(),
        "/chapter2/get/state": (context) => const GetStateObjectRoute(),
        "/chapter2/tap/box": (context) => const TapBoxPage(),
        "/chapter2/random/words": (context) => const RandomWordsRoute(),
        "/chapter2/assets/route": (context) => const AssetsRoute(),
        "/chapter2/debugger": (context) => const DebuggerRoute(),
        "/chapter2/tip/route": (context) => TipRoute(
            text: ModalRoute.of(context)!.settings.arguments.toString()),
        "/chapter3": (context) => const Chapter3Page(),
        "/chapter3/text/style": (context) => const TextStyleRoute(),
        "/chapter3/button": (context) => const ButtonRoute(),
        "/chapter3/image/icon": (context) => const ImageIconRoute(),
        "/chapter3/switch/checkbox": (context) =>
            const SwitchAndCheckBoxRoute(),
        "/chapter3/text/field": (context) => const TextFieldRoute(),
        "/chapter3/form/test": (context) => const FormTestRoute(),
        "/chapter3/progress": (context) => const ProgressIndicatorRoute(),
        "/chapter3/progress/animation": (context) =>
            const ProgressAnimationRoute(),
        "/chapter4": (context) => const Chapter4Page(),
        "/chapter4/constraints": (context) => const ConstraintsRoute(),
        "/chapter4/row/column": (context) => const RowAndColumnRoute(),
        "/chapter4/flex": (context) => const FlexRoute(),
        "/chapter4/wrap": (context) => const WrapRoute(),
        "/chapter4/stack/positioned": (context) =>
            const StackAndPositionedRoute(),
        "/chapter4/align": (context) => const AlignRoute(),
        "/chapter4/layout": (context) => LayoutRoute(),
        "/chapter5": (context) => const Chapter5Page(),
        "/chapter5/padding": (context) => const PaddingRoute(),
        "/chapter5/decorated/box": (context) => const DecoratedBoxRoute(),
        "/chapter5/transform": (context) => const TransformRoute(),
        "/chapter5/container": (context) => const ContainerRoute(),
        "/chapter5/clip": (context) => const ClipRoute(),
        "/chapter5/fitted/box": (context) => const FittedBoxRoute(),
        "/chapter5/scaffold": (context) => const ScaffoldRoute(),
        "/chapter6": (context) => const Chapter6Page(),
        "/chapter6/single/child/scroll/view": (context) =>
            const SingleChildScrollViewRoute(),
        "/chapter6/list/view1": (context) => const ListView1Route(),
        "/chapter6/list/view2": (context) => const ListView2Route(),
        "/chapter6/infinite/list/view": (context) => const InfiniteListView(),
        "/chapter6/scroll/controller": (context) =>
            const ScrollControllerTestRoute(),
        "/chapter6/scroll/notification": (context) =>
            const ScrollNotificationTestRoute(),
        "/chapter6/animated/list": (context) => const AnimatedListRoute(),
        "/chapter6/grid/view": (context) => const GridViewTestRoute(),
        "/chapter6/grid/view/2": (context) => const GridViewTestRoute2(),
        "/chapter6/grid/view/3": (context) => const GridViewTestRoute3(),
        "/chapter6/grid/view/4": (context) => const GridViewTestRoute4(),
        "/chapter6/infinite/grid/view": (context) => const InfiniteGridView(),
        "/chapter6/page/view": (context) => const PageViewTestRoute(),
        "/chapter6/keep/alive": (context) => const KeepAliveTest(),
        "/chapter6/tab/bar/view": (context) => const TabBarViewTestRoute(),
        "/chapter6/custom/scroll/view": (context) =>
            const CustomScrollViewTestRoute(),
        "/chapter6/custom/scroll/view2": (context) =>
            const CustomScrollViewTestRoute2(),
        "/chapter6/nested/scroll/view1": (context) =>
            const NestedScrollViewRoute(),
        "/chapter6/nested/scroll/view2": (context) =>
            const NestedTabBarView1Route(),
        "/chapter7": (context) => const Chapter7Page(),
        "/chapter7/willpop/scope": (context) => const WillPopScopeTestRoute(),
        "/chapter7/inherited/widget": (context) =>
            const InheritedWidgetTestRoute(),
        "/chapter7/value/listenable": (context) => const ValueListenableRoute(),
        "/chapter7/future/builder": (context) => const FutureBuilderRoute(),
        "/chapter7/stream/builder": (context) => const StreamBuilderRoute(),
        "/chapter7/alert/dialog": (context) => const AlertDialogRoute(),
        "/chapter8": (context) => const Chapter8Page(),
        "/chapter8/pointer/move/indicator": (context) =>
            const PointerMoveIndicatorRoute(),
        "/chapter8/gesture/recognizer": (context) =>
            const GestureRecognizerRoute(),
        "/chapter8/event/bus": (context) => const EventBusRoute(),
        "/chapter8/notification": (context) => const NotificationRoute(),

        "/chapter9": (context) => const Chapter9Page(),
        "/chapter9/scale/animation": (context) => const ScaleAnimationRoute(),
        "/chapter9/hero/animation": (context) => const HeroAnimationRoute(),
        "/chapter9/stagger/animation": (context) => const StaggerAnimationRoute(),
        "/chapter9/animation/switcher": (context) => const AnimatedSwitcherCounterRoute(),
        "/chapter9/animation/decorated": (context) => const AnimatedDecoratedRoute(),
        "/chapter9/animation/widgets": (context) => const AnimatedWidgetsRoute(),


        "/chapter10": (context) => const Chapter10Page(),
        "/chapter10/combination/widget": (context) => const CombinationWidgetRoute(),
        "/chapter10/turn/box": (context) => const TurnBoxRoute(),
        "/chapter10/custom/paint": (context) => const CustomPaintRoute(),
        "/chapter10/gradient/circular/progress": (context) => const GradientCircularProgressRoute(),

        "/chapter11": (context) => const Chapter11Page(),
        "/chapter11/file/operation": (context) => const FileOperationRoute(),
        "/chapter11/http/test": (context) => const HttpTestRoute(),
        "/chapter11/dio/test": (context) => const DioTestRoute(),
        "/chapter11/web/socket/test": (context) => const WebSocketTestRoute(),
        "/chapter11/socket/test": (context) => const SocketTestRoute(),
        "/chapter11/json/test": (context) => const JsonTestRoute(),


        "/chapter12": (context) => const Chapter12Page(),
        "/chapter13": (context) => const Chapter13Page(),
        "/chapter14": (context) => const Chapter14Page(),
        "/chapter15": (context) => const Chapter15Page(),
      },
      initialRoute: "/",
    );
  }
}

class IndexPage extends StatelessWidget {
  final logger = Logger(printer: PrettyPrinter());

  final String title;

  IndexPage({Key? key, required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.all(10),
        child: GridView.count(
          crossAxisCount: 2,
          mainAxisSpacing: 10,
          crossAxisSpacing: 10,
          childAspectRatio: 1.5,
          children: <Widget>[
            TitleButton(
                title: "第一章：起步",
                onPressed: () => Navigator.of(context).pushNamed("/chapter1")),
            TitleButton(
                title: "第二章：第一个Flutter应用",
                onPressed: () => Navigator.of(context).pushNamed("/chapter2")),
            TitleButton(
                title: "第三章：基础组件",
                onPressed: () => Navigator.of(context).pushNamed("/chapter3")),
            TitleButton(
                title: "第四章：布局类组件",
                onPressed: () => Navigator.of(context).pushNamed("/chapter4")),
            TitleButton(
                title: "第五章：容器类组件",
                onPressed: () => Navigator.of(context).pushNamed("/chapter5")),
            TitleButton(
                title: "第六章：可滚动组件",
                onPressed: () => Navigator.of(context).pushNamed("/chapter6")),
            TitleButton(
                title: "第七章：功能型组件",
                onPressed: () => Navigator.of(context).pushNamed("/chapter7")),
            TitleButton(
                title: "第八章：事件处理与通知",
                onPressed: () => Navigator.of(context).pushNamed("/chapter8")),
            TitleButton(
                title: "第九章：动画",
                onPressed: () => Navigator.of(context).pushNamed("/chapter9")),
            TitleButton(
                title: "第十章：自定义组件",
                onPressed: () => Navigator.of(context).pushNamed("/chapter10")),
            TitleButton(
                title: "第十一章：文件操作与网络请求",
                onPressed: () => Navigator.of(context).pushNamed("/chapter11")),
            TitleButton(
                title: "第十二章：Flutter 扩展",
                onPressed: () => Navigator.of(context).pushNamed("/chapter12")),
            TitleButton(
                title: "第十三章：国际化",
                onPressed: () => Navigator.of(context).pushNamed("/chapter13")),
            TitleButton(
                title: "第十四章：Flutter核心原理",
                onPressed: () => Navigator.of(context).pushNamed("/chapter14")),
            TitleButton(
                title: "第十五章：一个完整的Flutter应用",
                onPressed: () => Navigator.of(context).pushNamed("/chapter15")),
          ],
        ),
      ),
    );
  }
}
